package com.kaikeba.dao;

import com.kaikeba.bean.Skill;
import com.kaikeba.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SkillDaoImp implements BaseDao<Skill> {

    private static final String SQL_INSERT = "insert into kkb_skill(userid,keywords) values(?,?)";
    private static final String SQL_FIND_BY_USERID = "select * from kkb_skill where userid=?";

    @Override
    public int insert(Skill skill) {
        Connection conn = DBUtil.getConn();
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            state = conn.prepareStatement(SQL_INSERT);
            state.setInt(1, skill.getUserId());
            state.setString(2, skill.getKeyWords());
            int row = state.executeUpdate();
            return row > 0 ? row : -1;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, state, rs);
        }
        return -1;
    }

    @Override
    public Skill findByUserId(int userId) {
        Connection conn = DBUtil.getConn();
        PreparedStatement state = null;
        ResultSet rs = null;
        Skill skill = null;
        try {
            state = conn.prepareStatement(SQL_FIND_BY_USERID);
            state.setInt(1, userId);
            rs = state.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String keywords = rs.getString("keywords");
                skill = new Skill(id, userId, keywords);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, state, rs);
        }
        return skill;
    }
}
